/*
 * CatalogueBean.java
 *
 * Created on 27 March 2007, 13:17
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import usyd.comp5348.Catalogue;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Yuezhong Zhang
 */
@Stateless(mappedName="ejb/Catalogue")
public class CatalogueBean implements CatalogueRemote, CatalogueLocal {
    
    @PersistenceContext
    EntityManager em;
    
    /** Creates a new instance of CatalogueBean */
    public CatalogueBean() {
    }
    
    public int createVideo(CatalogueInfo catalogue)  throws CatalogueException{
        try {
            Catalogue c = new Catalogue(catalogue);
            em.persist(c);
            em.flush();
            catalogue.setId(c.getId());
            return catalogue.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not store new Catalogue", e);
        }
    }
    
    public void modifyVideo(CatalogueInfo catalogue)  throws CatalogueException{
        try {
            Catalogue c = new Catalogue(catalogue);
            em.merge(c);
            catalogue.setId(c.getId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not modify existing Catalogue", e);
        }
    }
    
    public void deleteVideo(int id)  throws CatalogueException{
        Catalogue c=null;
        try {
            c = em.find(Catalogue.class, id);
            if (c!=null)
                em.remove(c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not remove Catalogue by Id", e);
        }
        if (c==null)
            throw new CatalogueException("Could not removed video(" + id + ") - does not exist");
    }
    
    public CatalogueInfo getVideoById(int id)  throws CatalogueException{
        try {
            Catalogue c = em.find(Catalogue.class, id);
            if (c==null)
                return null;
            return c.getCatalogueInfo();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not retrieve Catalogue by Id", e);
        }
    }
    
    public CatalogueInfo getVideoByCatalogueNumber(String catalogueNumber)  throws CatalogueException{
        try {
            Catalogue c = (Catalogue)em.createNamedQuery("getVideoByCatalogueNumber").setParameter("catalogueNumber", catalogueNumber).getSingleResult();
            if (c==null)
                return null;
            return c.getCatalogueInfo();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not retrieve Catalogue by catalogueNumber", e);
        }
    }
    
    public List<CatalogueInfo> getVideosByDirector(String director)  throws CatalogueException{
        try {
            List<Catalogue> c = em.createNamedQuery("getVideosByDirector").setParameter("director", director).getResultList();
            if (c==null)
                return null;
            List<CatalogueInfo> cl = new ArrayList<CatalogueInfo>();
            for (Catalogue cat : c) {
                cl.add(cat.getCatalogueInfo());
            }
            return cl;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not retrieve Catalogue by director", e);
        }
    }
    
    public  List<CatalogueInfo> getVideosByGenre(String genre) throws CatalogueException{
        try {
            List<Catalogue> c = em.createNamedQuery("getVideosByGenre").setParameter("genre", genre).getResultList();
            if (c==null)
                return null;
            List<CatalogueInfo> cl = new ArrayList<CatalogueInfo>();
            for (Catalogue cat : c) {
                cl.add(cat.getCatalogueInfo());
            }
            return cl;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CatalogueException("Could not retrieve Catalogue by genre", e);
        }
    }
    
}
